package com.live.stamp.web.stamp.baseDataService.impl;

import com.hikeSoft.common.utils.BaseUtils;
import com.live.stamp.base.common.PageVO;
import com.live.stamp.base.service.impl.BaseSupportServiceImpl;
import com.live.stamp.web.stamp.baseDataService.StampDataService;
import com.live.stamp.web.stamp.model.TStampComment;
import com.live.stamp.web.stamp.model.TStampInfo;
import com.live.stamp.web.stamp.model.TStampInfoPic;
import com.live.stamp.web.stamp.model.TStampLike;
import com.live.stamp.web.stamp.service.impl.StampServiceImpl;
import com.live.stamp.web.stamp.vo.TStampInfoVo;
import org.springframework.boot.container.core.common.BusinessException;
import org.springframework.boot.container.core.log.ZKLogger;
import org.springframework.boot.container.core.log.ZKLoggerFactory;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author cwd E-mail: wendong.cao@jsxfedu.com
 * @version 1
 * @date 2018/4/13 15:02
 */
@Service
public class StampDataServiceImpl extends BaseSupportServiceImpl implements StampDataService {

    private static final ZKLogger logger = ZKLoggerFactory.getLogger(StampServiceImpl.class);


    @Override
    public PageVO<TStampInfoVo> listStampInfoPage(TStampInfoVo tStampInfoVo) throws BusinessException {

        return null;
    }

    @Override
    public int addStamp(TStampInfo tStampInfo) throws BusinessException {
        logger.info("enter addStamp method , param tStampInfo:{}", tStampInfo);
        try {
            return this.getDao().insert(tStampInfo);
        } catch (SQLException e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            throw new BusinessException("插入印记失败。" + e.getMessage());
        }
    }

    @Override
    public int deleteStamp(String id) throws BusinessException {
        logger.info("enter deleteStamp method , param id:{}", id);
        try {

            String sql = "update t_inji_info set state='0' where id=:id";
            Map<String, Object> param = new HashMap<>();
            param.put("id", id);

            return this.getDao().exec(sql, param);

        } catch (SQLException e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            throw new BusinessException("删除印迹失败。" + e.getMessage());
        }
    }

    @Override
    public int[] addStampPic(List<TStampInfoPic> tStampInfoPics) throws BusinessException {
        logger.info("enter addStampPic method , param tStampInfoPics:{}", tStampInfoPics);
        try {
            return this.getDao().insertBatch(tStampInfoPics);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            throw new BusinessException("插入印记照片失败。" + e.getMessage());
        }
    }

    @Override
    public int addStampLikeCount(String stampId) throws BusinessException {
        logger.info("enter addStampLikeCount method , param stampId:{}", stampId);
        try {
            String sql = "update t_inji_info set likeCount = likeCount+1 where id= :id";
            Map<String, Object> param = new HashMap<>();
            param.put("id", stampId);

            return this.getDao().exec(sql, param);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            throw new BusinessException("点赞印记失败。" + e.getMessage());
        }
    }

    @Override
    public int addStampLookCount(String stampId) throws BusinessException {
        logger.info("enter addStampLookCount method , param stampId:{}", stampId);
        try {
            String sql = "update t_inji_info set lookCount = lookCount+1 where id= :id";
            Map<String, Object> param = new HashMap<>();
            param.put("id", stampId);

            return this.getDao().exec(sql, param);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            throw new BusinessException("查看印记失败。" + e.getMessage());
        }
    }

    @Override
    public int addStampCommentCount(String stampId) throws BusinessException {
        logger.info("enter addStampCommentCount method , param stampId:{}", stampId);
        try {
            String sql = "update t_inji_info set commentCount = commentCount+1 where id= :id";
            Map<String, Object> param = new HashMap<>();
            param.put("id", stampId);

            return this.getDao().exec(sql, param);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            throw new BusinessException("品论印记失败。" + e.getMessage());
        }
    }

    @Override
    public int addStampLike(TStampLike tStampLike) throws BusinessException {
        logger.info("enter addStampLike method , param tStampLike:{}", tStampLike);
        try {
            return this.getDao().insert(tStampLike);
        } catch (SQLException e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            throw new BusinessException("插入点赞信息失败。" + e.getMessage());
        }
    }

    @Override
    public int addStamppComment(TStampComment tStampComment) throws BusinessException {
        logger.info("enter addStamppComment method , param tStampComment:{}", tStampComment);
        try {
            return this.getDao().insert(tStampComment);
        } catch (SQLException e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            throw new BusinessException("插入评论信息失败。" + e.getMessage());
        }
    }

    @Override
    public TStampInfoVo getStampInfo(TStampInfoVo tStampInfoVo) throws BusinessException {
        logger.debug("enter into getStampInfo method. params tStampInfoVo{}", tStampInfoVo);
        Map<String,Object> params = new HashMap<>();

        String sql = "SELECT i.*, u.photoUrl, u.userName, u.signature FROM t_inji_info i,t_user u WHERE i.tUserId=u.id  "+ this.searchParma(tStampInfoVo, params);

        try{
            return this.getDao().selectOne(TStampInfoVo.class, sql, params);
        }catch(Exception e){
            logger.error(e.getMessage());
            throw new BusinessException(e.toString());
        }
    }

    @Override
    public List<Map> listLikeByStampId(String stampId) throws BusinessException {
        logger.debug("enter into listLikeByStampId method. params stampId{}", stampId);
        Map<String,Object> params = new HashMap<>();

        String sql = "select l.*,u.photoUrl, u.userName from t_inji_like l, t_user u  where l.tUserId=u.id and l.tInjiInfoId=:stampId order by l.createTime desc";
        params.put("stampId", stampId);

        try{
            return this.getDao().selectList(Map.class, sql, params);
        }catch(Exception e){
            logger.error(e.getMessage());
            throw new BusinessException(e.toString());
        }
    }

    @Override
    public List<Map> listCommentByStampId(String stampId) throws BusinessException {
        logger.debug("enter into listCommentByStampId method. params stampId{}", stampId);
        Map<String,Object> params = new HashMap<>();

        String sql = "select l.*,u.photoUrl, u.userName from t_inji_comment l, t_user u  where l.tUserId=u.id and l.tInjiInfoId=:stampId order by l.createTime asc";
        params.put("stampId", stampId);

        try{
            return this.getDao().selectList(Map.class, sql, params);
        }catch(Exception e){
            logger.error(e.getMessage());
            throw new BusinessException(e.toString());
        }
    }

    @Override
    public List<TStampInfoPic> lisStampPicsByStampId(String stampId) throws BusinessException {
        logger.debug("enter into lisStampPicsByStampId method. params stampId{}", stampId);
        Map<String,Object> params = new HashMap<>();

        String sql = "select * from t_inji_info_pic where tInjiInfoId=:stampId order by createTime desc ";
        params.put("stampId", stampId);

        try{
            return this.getDao().selectList(TStampInfoPic.class, sql, params);
        }catch(Exception e){
            logger.error(e.getMessage());
            throw new BusinessException(e.toString());
        }
    }

    private String searchParma(TStampInfoVo tStampInfoVo, Map<String, Object> param){
        if(tStampInfoVo == null){
            throw new BusinessException("查询印记失败。");
        }
        String sql = "";
        if(BaseUtils.isNotBlank(tStampInfoVo.getId())){
            sql += " and i.ID = :id";
            param.put("id",tStampInfoVo.getId());
        }

        return sql;
    }
}
